﻿using Infrastructure.Dao;
using SqlSugar;
using SyntacticSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.ViewModels;
using DormMS.Model.ViewModels.Activity;

namespace DormMS.Service
{
    public class ActivityTeamMembersService : IActivityTeamMembersService
    {
        private SqlSugarClient db;

        /// <summary>
        /// 获取我参与的一分钱活动
        /// </summary>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="memberid"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IEnumerable<V_ActivityTeamMembers> GetActivityTeamMembersList(int skip, int take, int memberid, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                var list = db.Queryable<Model.ActivityTeamMembers>()
                    .OrderBy(o => o.JoinTime, OrderByType.Desc).Where(m => m.UserId == memberid)
                    .ToPageList(skip / take + 1, take, ref total)
                    .Select(s => new V_ActivityTeamMembers()
                    {
                        ActivityId = s.ActivityId,
                        UserId = s.UserId,
                        JoinTime = s.JoinTime,
                        ActivityName = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().ActivityName,
                        Id = s.Id,
                        ActivityTeamId = s.ActivityTeamId,
                        CoverImg = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().CoverImg,
                        IsDeleted = s.IsDeleted,
                        IsHead = s.IsHead,
                        OrderNo = s.OrderNo,
                        PaymentAmount = s.PaymentAmount,
                        RefundStatus = s.RefundStatus,
                        TeamStatus = db.Queryable<Model.ActivityTeam>().Where(t => t.Id == s.ActivityTeamId).FirstOrDefault().TeamStatus,
                        UserHeadImg = s.UserHeadImg,
                        UserName = s.UserName,
                        WinningStatus = s.WinningStatus,
                        ActivityTeamContinuedTime = db.Queryable<Model.ActivityTeam>().Where(t => t.Id == s.ActivityTeamId).FirstOrDefault().ContinuedTime,
                        ActivityTeamCreateTime = db.Queryable<Model.ActivityTeam>().Where(t => t.Id == s.ActivityTeamId).FirstOrDefault().CreateTime,
                        DrawId = s.DrawId,
                        DrawRewardName = s.DrawRewardName,
                        LuckDrawID = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().LuckDrawID
                    }).ToList();
                return list;
            }
        }

        public V_ActivityTeamMembers GetActivityTeamMembersInfo(int Id, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                var list = db.Queryable<Model.ActivityTeamMembers>().OrderBy(o => o.JoinTime, OrderByType.Desc)
                    .Where(m => m.Id == Id)
                    .ToPageList(0 / 5 + 1, 5, ref total)
                    .Select(s => new V_ActivityTeamMembers()
                    {
                        LuckDrawID = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().LuckDrawID,
                        ActivityId = s.ActivityId,
                        ActivityTeamId = s.ActivityTeamId,
                        Id = s.Id,
                        IsHead = s.IsHead,
                        TeamStatus = db.Queryable<Model.ActivityTeam>().Where(t => t.Id == s.ActivityTeamId).FirstOrDefault().TeamStatus,
                        WinningStatus = s.WinningStatus,
                        UserId = s.UserId,
                        IsWinning = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().IsWinning,
                        OrderNo = s.OrderNo,
                        PaymentAmount = s.PaymentAmount
                    }).ToList()[0];
                return list;
            }
        }

        /// <summary>
        /// 修改团成员表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditActivityTeamMembers(Model.ActivityTeamMembers model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.ActivityTeamMembers>(model);
            }
        }
        /// <summary>
        /// 根据订单号获取团信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.ActivityTeamMembers GetActivityTeamMembersInfo(string orderno)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Model.ActivityTeamMembers>().SingleOrDefault(t => t.OrderNo == orderno);
                return model;
            }
        }

        public ActivityTeamMembers FindActivityTeamMembersInfo(string where)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Model.ActivityTeamMembers>().Where(where).FirstOrDefault();
                return model;
            }
        }

        /// <summary>
        /// 获取用来推送消息的用户列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<ActMemberVM> GetMembersByTeamId(int Id)
        {
            using(db = DbConfig.GetInstance())
            {
                string sql = $@"select t2.NickName,t2.OpenId,t2.Id from ActivityTeamMembers t1
                        left join(
                        select * from Member
                        )as t2 on t1.UserId=t2.Id where t1.ActivityTeamId={Id}";
                return db.GetDataTable(sql).TryToList<ActMemberVM>();
            }
        }
    }
}
